<%--
  Created by IntelliJ IDEA.
  User: wens
  Date: 2017/3/7
  Time: 下午6:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>测试微信登录</title>
    <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
openId:<p id="openId"></p><br>
nickname:<p id="nickname"></p><br>
province:<p id="province"></p><br>
city:<p id="city"></p><br>
avatar:
<p >
<img src="" id="avatar" >
</p><br>

<script>

    function getQueryString (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return r[2]; return null;
    }

    $(function(){
        var openId  = getQueryString("openId");
        if(openId){
            var openId = getQueryString("openId")
            var nickname = decodeURIComponent( getQueryString("nickname") )
            var province = decodeURIComponent(getQueryString("province"))
            var city = decodeURIComponent( getQueryString("city"))
            var avatar = decodeURIComponent( getQueryString("headImgUrl"))

            $("#openId").text(openId)
            $("#nickname").text(nickname)
            $("#province").text(province)
            $("#city").text(city )
            $("#avatar").attr("src" ,avatar )

            $.post('http://localhost:8080/api/m/wx/login',{
                openId:openId,
                nickname:nickname,
                province:province,
                city:city,
                avatar:avatar
            },function(ret){
                if(ret.success){
                    alert('Login success')
                }else{
                    alert('Sorry,login fail')
                }
            },'json')

        }else{
            document.location.href = 'http://wx.yuyou100.com/wechat/oauth2?redirectUrl='+ encodeURIComponent(location.href)
        }
    })



</script>
</body>
</html>
